/*
 * Open LiveReplay Server - Live replay server for Supreme Commander
 * 
 * Copyright (C) 2009, 2009 Open LiveReplay Server Project
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details:
 * http://www.gnu.org/copyleft/lesser.html#TOC3
 */
package net.galacticwar.lua;

import java.io.IOException;
/**
 * 
 * Provides methods for parsing LUA files
 * 
 * @author Yoann Meste (aka Mancer)
 *
 */
public class LuaDeserialize {
	private static final int TYPE_NUMBER = 0;
	private static final int TYPE_STRING = 1;
	private static final int TYPE_NIL = 2;
	private static final int TYPE_BOOLEAN = 3;
	private static final int TABLE_BEGIN = 4;
	private static final int TABLE_END = 5;


	
	private static int peekType( LittleEndianInputStream in ) throws IOException
	{
		if (in.markSupported()) {
			in.mark(10);
			int type = in.read();
			in.reset();
			return type;
		}
		else{
			throw new RuntimeException("Mark are not supported");
		}

		
	} 
	
	public static Object parseLua (LittleEndianInputStream in) throws IOException, LuaException{

	    int type = in.read();
	    switch( type )
	    {
	        case TYPE_NIL:
	        {
	        	return null;
	        }
	        case TYPE_BOOLEAN:
	        {
	        	
	            boolean val = in.read() != 0;
	            return val;
	        }
	        case TYPE_NUMBER:
	        {
	            float val = in.readFloat();
	            return val;
	        }
	        case TYPE_STRING:
	        {
	        	
	            String s = in.readString();
	            return s;
	        }
	        case TABLE_BEGIN:
	        {
	        	LuaTable table = new LuaTable();

	            while (peekType(in) != TABLE_END)
	            {
	                Object key = parseLua(in);
	                Object value = parseLua(in);
	                LuaPair pair = new LuaPair();
	                pair.push(key);
	                pair.setEqual(true);
	                pair.push(value);
	                
	                table.add(pair);
	            }
	            int endTable = in.read();
	            assert(endTable == TABLE_END);
	            return table;

	        }
	        
	        case TABLE_END:
	        {
	        	throw new LuaException("Error: unexpected end-of-table");

	        }
	        default:
	        	throw new LuaException("Unknown lua data");
	    }
	}
}
